package com.fan.teamer.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.fan.teamer.model.domain.Team;

public class TeamDaoImpl implements TeamDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Team getTeam(int id) {
		Team team = (Team) sessionFactory.getCurrentSession().get(Team.class,
				id);
		return team;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Team> getAllTeams() {
		return sessionFactory.getCurrentSession().createCriteria(Team.class)
				.list();
	}

	@Override
	public int getSize() {
		return getAllTeams().size();
	}

	@Override
	public void addTeam(Team team) {
		sessionFactory.getCurrentSession().save(team);
	}

	@Override
	public void updateTeam(Team team) {
		sessionFactory.getCurrentSession().update(team);
	}

	@Override
	public void deleteTeam(Team team) {
		sessionFactory.getCurrentSession().delete(team);
	}

	@Override
	public boolean hasTeam(int id) {
		if (getTeam(id) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public List<String> getColumnNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Team> getTeamsByCountry(String country) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Team.class);
		criteria.add(Restrictions.eq("country", country));
		List<Team> teams = (List<Team>) criteria.list();
		return teams;
	}


}
